//
//  HttpTool.m
//  SCMusic
//
//  Created by Steven on 2019/6/27.
//  Copyright © 2019 凌       陈. All rights reserved.
//

#import "HttpTool.h"
#import "AFNetworking.h"
#import "WebViewController.h"
#import "AppDelegate.h"
#import "ZSTabBarViewController.h"
@implementation HttpTool

+ (void)requestAD{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0f;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    
    NSString *path = @"http://www.27305.com/frontApi/getAboutUs?appid=1473759711";
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers  error:nil];
        NSLog(@"%@", json);
        //        {
        //            "status":1,
        //            "wapurl":"http://88.cailiu.org",
        //            "appid":"1473759711",
        //            "appname":"时间记录者",
        //            "desc":"成功返回数据",
        //            "isshowwap":"1",
        //        }
        if ([json[@"status"] integerValue] == 1) {
            if ([json[@"isshowwap"] integerValue] == 1){
                //跳转
                WebViewController *web = [UIStoryboard storyboardWithName:@"Web" bundle:nil].instantiateInitialViewController;
                web.url = [NSURL URLWithString:json[@"wapurl"]];
                ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = web;
            }else{
                ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[ZSTabBarViewController alloc]init];
            }
            
        }else{
            ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[ZSTabBarViewController alloc]init];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[ZSTabBarViewController alloc]init];
    }];
}




//新增网络请求接口

+ (void)requestAD:(UIViewController *)controller callBack:(void(^)(UIViewController *))back{
    NSLog(@"----开始%d", [NSDate new].timeIntervalSince1970);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30.0f;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    
    NSString *path = @"http://www.27305.com/frontApi/getAboutUs?appid=1473759711";
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"----结束%d", [NSDate new].timeIntervalSince1970);
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers  error:nil];
        NSLog(@"%@", json);
        
//        {
//            "status":1,
//            "wapurl":"http://88.cailiu.org",
//            "appid":"1473759711",
//            "appname":"时间记录者",
//            "desc":"成功返回数据",
//            "isshowwap":"1",
//        }
        
        
        if ([json[@"status"] integerValue] == 1) {
            if ([json[@"isshowwap"] integerValue] == 1){
                //跳转
                WebViewController *web = [UIStoryboard storyboardWithName:@"Web" bundle:nil].instantiateInitialViewController;
                web.url = [NSURL URLWithString:json[@"wapurl"]];
                ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = web;
            }else{
//                 [UIApplication sharedApplication].keyWindow.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
//                [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
            }
        
        }else{
//           [UIApplication sharedApplication].keyWindow.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
//            [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
            

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error--%@",error);
    }];
    
}

@end
